package com.mall.auth.config;

import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * 邮件配置类
 */
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class MailConfig {

    /**
     * 配置JavaMailSender
     *
     * @param mailProperties 邮件配置属性
     * @return JavaMailSender实例
     */
    @Bean
    public JavaMailSender javaMailSender(MailProperties mailProperties) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(mailProperties.getHost());
        sender.setPort(mailProperties.getPort());
        sender.setUsername(mailProperties.getUsername());
        sender.setPassword(mailProperties.getPassword());
        
        Properties props = sender.getJavaMailProperties();
        props.putAll(mailProperties.getProperties());
        
        return sender;
    }
}